package com.example.eldersystem.controller;

import com.example.eldersystem.response.DataResponse;
import com.example.eldersystem.operatedata.model.OldPerson;
import com.example.eldersystem.operatedata.service.OldPersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;


@RestController
@RequestMapping("/oldperson")
public class OldPersonController {

    @Autowired
    OldPersonService oldPersonService;

    @RequestMapping("/add")
    private DataResponse<String> addOldPerson(@RequestBody Map<String,Object> map){

        DataResponse<String> dataResponse = new DataResponse<>();
        OldPerson oldPerson = new OldPerson();
        Date udate = null;
        Date checkIn =null;
        Date checkOut = null;

        String birthday = (String) map.get("birthday");
        try{
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            udate = format.parse(birthday);
        }catch (ParseException e){
            dataResponse.setCode(10);
            dataResponse.setMsg("日期格式有误");
            return dataResponse;
        }
        oldPerson.setBirthday(udate);


        String checkInDate = map.get("checkInDate").toString();
        try{
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            checkIn = format.parse(checkInDate);
        }catch (ParseException e){
            dataResponse.setCode(10);
            dataResponse.setMsg("入院日期格式有误");
            return dataResponse;
        }
        oldPerson.setCheckInDate(checkIn);
        System.out.println(checkInDate);


        if(map.get("checkOutDate")!=null) {
            String checkOutDate = map.get("checkOutDate").toString();
            if (!checkOutDate.equals("")) {
                try {
                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                    checkOut = format.parse(checkOutDate);
                } catch (ParseException e) {
                    dataResponse.setCode(10);
                    dataResponse.setMsg("出院日期格式有误");
                    return dataResponse;
                }
                if (checkOut.after(checkIn)) {
                    oldPerson.setCheckOutDate(checkOut);
                } else {
                    dataResponse.setMsg("出院日期不得早于等于入院时期");
                    return dataResponse;
                }
            }
        }
        oldPerson.setDescription(map.get("description").toString());
        oldPerson.setUserName(map.get("userName").toString());
        oldPerson.setGender(map.get("gender").toString());
        oldPerson.setIdCard(map.get("idCard").toString());
        oldPerson.setPhone(map.get("phone").toString());
        oldPerson.setImgsetDir(map.get("imgsetDir").toString());
        oldPerson.setProfilePhoto(map.get("profilePhoto").toString());
        oldPerson.setFirstGuardianName(map.get("firstGuardianName").toString());
        oldPerson.setFirstGuardianRelationship(map.get("firstGuardianRelationship").toString());
        oldPerson.setFirstGuardianPhone(map.get("firstGuardianPhone").toString());
        oldPerson.setFirstGuardianWechat(map.get("firstGuardianWechat").toString());
        oldPerson.setSecGuardianName(map.get("secGuardianName").toString());
        oldPerson.setSecGuardianRelationship(map.get("secGuardianRelationship").toString());
        oldPerson.setSecGuardianPhone(map.get("secGuardianPhone").toString());
        oldPerson.setSecGuardianWechat(map.get("secGuardianWechat").toString());
        oldPerson.setRoomNumber(map.get("roomNumber").toString());
        oldPerson.setHealthState(map.get("healthState").toString());
        dataResponse = oldPersonService.addOldPerson(oldPerson);
        return dataResponse;
    }


    @RequestMapping("/delete")
    private DataResponse<String> deleteOldPerson(@RequestBody Map<String,Object> map){
        DataResponse<String> dataResponse = new DataResponse<>();
        int id = Integer.parseInt(map.get("id").toString());
        dataResponse = oldPersonService.deleteOldPerson(id);
        return dataResponse;
    }

    @RequestMapping("/update")
    private DataResponse<String> updateOldPerson(@RequestBody Map<String,Object> map){
        DataResponse<String> dataResponse = new DataResponse<>();
        OldPerson oldPerson = new OldPerson();
        Date date = null;
        Date checkIn =null;
        Date checkOut = null;
        String birthday = map.get("birthday").toString();
        String checkInDate = map.get("checkInDate").toString();
        dataResponse.setCode(10);
        try{
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            date = format.parse(birthday);
        }catch (ParseException e){
            dataResponse.setMsg("生日日期格式有误");
            return dataResponse;
        }
        oldPerson.setBirthday(date);
        try{
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            checkIn = format.parse(checkInDate);
        }catch (ParseException e){
            dataResponse.setMsg("入院日期格式有误");
            return dataResponse;
        }
        oldPerson.setCheckInDate(checkIn);
        if(map.get("checkOutDate")!=null) {
            String checkOutDate = map.get("checkOutDate").toString();
            if (!checkOutDate.equals("")) {
                try {
                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                    checkOut = format.parse(checkOutDate);
                } catch (ParseException e) {
                    dataResponse.setMsg("出院日期格式有误");
                    return dataResponse;
                }
                if (checkOut.after(checkIn)) {
                    oldPerson.setCheckOutDate(checkOut);
                } else {
                    dataResponse.setMsg("出院日期不得早于等于入院时期");
                    return dataResponse;
                }
            }
        }

        System.out.println(map.get("id"));
        oldPerson.setId(Integer.parseInt(map.get("id").toString()));
        oldPerson.setDescription(map.get("description").toString());
        oldPerson.setUserName(map.get("userName").toString());
        oldPerson.setGender(map.get("gender").toString());
        oldPerson.setIdCard(map.get("idCard").toString());
        oldPerson.setPhone(map.get("phone").toString());
        oldPerson.setImgsetDir(map.get("imgsetDir").toString());
        oldPerson.setProfilePhoto(map.get("profilePhoto").toString());
        oldPerson.setFirstGuardianName(map.get("firstGuardianName").toString());
        oldPerson.setFirstGuardianRelationship(map.get("firstGuardianRelationship").toString());
        oldPerson.setFirstGuardianPhone(map.get("firstGuardianPhone").toString());
        oldPerson.setFirstGuardianWechat(map.get("firstGuardianWechat").toString());
        oldPerson.setSecGuardianName(map.get("secGuardianName").toString());
        oldPerson.setSecGuardianRelationship(map.get("secGuardianRelationship").toString());
        oldPerson.setSecGuardianPhone(map.get("secGuardianPhone").toString());
        oldPerson.setSecGuardianWechat(map.get("secGuardianWechat").toString());
        oldPerson.setRoomNumber(map.get("roomNumber").toString());
        oldPerson.setHealthState(map.get("healthState").toString());
        dataResponse = oldPersonService.updateOldPerson(oldPerson);
        return dataResponse;
    }

    @RequestMapping("/count")
    private DataResponse<Integer> getOldPersonNum(){
        DataResponse<Integer> dataResponse = new DataResponse<>();
        dataResponse.setData(oldPersonService.getOldPersonNum());
        return dataResponse;
    };

    @RequestMapping("/select")
    private DataResponse<List<OldPerson>> getOldPerson(@RequestBody Map<String,Object> map){
        DataResponse<List<OldPerson>> dataResponse = new DataResponse<>();
        int from = 0;
        int num = 0;
        try{
            from = Integer.parseInt(map.get("from").toString());
            num = Integer.parseInt(map.get("num").toString());
        }catch(Exception e){
            dataResponse.setCode(1);
            dataResponse.setMsg("请求错误");
            return dataResponse;
        }
        dataResponse=oldPersonService.selectOldPerson(from,num);
        return dataResponse;
    };

    @RequestMapping("/search")
    private DataResponse<List<OldPerson>> searchOldPerson(@RequestBody Map<String,Object> map){
        DataResponse<List<OldPerson>> dataResponse = new DataResponse<>();
        String userName;
        try{
            userName = map.get("userName").toString();
        }catch(Exception e){
            dataResponse.setCode(1);
            dataResponse.setMsg("请求错误");
            return dataResponse;
        }
        dataResponse=oldPersonService.selectOldPersonByName(userName);
        return dataResponse;
    };

    @RequestMapping("/changePhoto")
    private DataResponse<String> updateLogoImage(@RequestBody Map<String,Object> map){
        DataResponse<String> dataResponse = new DataResponse<>();
        dataResponse.setCode(0);
        int id=Integer.parseInt(map.get("id").toString());
        String profilePhoto=map.get("profilePhoto").toString();
        dataResponse = oldPersonService.updateProfile(id,profilePhoto);
        return dataResponse;
    }
}
